/**
 * GroupObj
 *
 */
if(typeof (module) !="undefined"){
    module.exports = SubTemplateObj;

    var GroupObj = require('./GroupObj');
    var Serializer = require('./Serializer');
    var SubTemplateDS = require('../../../backServer/routes/pdf/ds/SubTemplateDS');
    var DS = require('../../../backServer/routes/pdf/ds/DS');
    var ImageDS = require('../../../backServer/routes/pdf/ds/ImageDS');
    var TextDS = require('../../../backServer/routes/pdf/ds/TextDS');
}



function SubTemplateObj()
{
    GroupObj.call(this);    // 继承

    this.ct = Serializer.prototype.CLASS_TYPE_SUBTEMPLATEOBJ;
}

SubTemplateObj.prototype = new GroupObj(); // 继承


// 取得子模板对象(返回subTemplateObj数组)
SubTemplateObj.prototype.getSubTemplateObj= function(key)
{
    var ret = new Array();
    // 如果没传入key，则返回该对象
    if(typeof key==undefined || typeof key=="undefined"){
        ret.push(this);
    }else if( this.tn == key) //  如果传入了key，则tn匹配后，才返回该对象
        ret.push(this);
    return ret;
}


// 取得子模板对象(返回subTemplateObj数组)
SubTemplateObj.prototype.getSubObjOfDS= function()
{
    var ret = new Array();
    var leafs = this.getLeafObjs();
    for(var i=0;i<leafs.length;i++){
        var leaf = leafs[i];
        if(leaf.tn && leaf.tn!=""){
            if(leaf.ct == Serializer.prototype.CLASS_TYPE_IMAGEOBJ){
                ret.push(leaf);
            }else if(leaf.ct == Serializer.prototype.CLASS_TYPE_TEXTOBJ){
                ret.push(leaf);
            }
        }
    }
    return ret;
}



/**
 * 创建空白数据源
 * 返回  SubTemplateDS  （如果不含有SubTemplateObj对象，则返回null）
 */
SubTemplateObj.prototype.newEmptySubTemplateDS = function(){
    var ret = new SubTemplateDS();
    ret.tn = this.tn;

    var leafObjs = this.getLeafObjs();
    for(var i=0;i<leafObjs.length;i++){
        var leaf = leafObjs[i];

        if(leaf.tn && leaf.tn!=""){
            if(leaf.ct == Serializer.prototype.CLASS_TYPE_IMAGEOBJ){
                var ids = new ImageDS();
                ids.tn = leaf.tn;
                ret.content.push(ids);
            }else if(leaf.ct == Serializer.prototype.CLASS_TYPE_TEXTOBJ){
                var ids = new TextDS();
                ids.tn = leaf.tn;
                ids.fn = leaf.fn;// 用于从服务器取字轮廓
                ids.fs = leaf.fs;
                ids.bi = leaf.bi;
                ret.content.push(ids);
            }

        }
    }

    return ret;
}

/**
 * 将自身转换为GroupObj
 */
SubTemplateObj.prototype.toGroupObj = function(){
    this.ct = Serializer.prototype.CLASS_TYPE_GROUPOBJ;
}

/**
 * 将某一个数据源绑定进来
 */
SubTemplateObj.prototype.bindData = function(d){
    var subOfDS =this.getSubObjOfDS();
    for(var i=0;i<subOfDS.length;i++){
        var subObj = subOfDS[i];
        for(var j=0;j< d.content.length;){
            var ids = d.content[j];
            if(ids.tn == subObj.tn){
                if(subObj.ct == Serializer.prototype.CLASS_TYPE_IMAGEOBJ){
                    subObj.fp = ids.value;
                }else if(subObj.ct == Serializer.prototype.CLASS_TYPE_TEXTOBJ){
                    subObj.te = ids.value;
                    subObj.di = ids.di;
                }
                d.content.splice(j,1); // 删除第j个数据，防止重复赋值
                break;
            }else{
                j++;
            }
        }


    }
}


